{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>NPOI, 2.7.2</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: NPOI\"\n",
    "\n",
    "using NPOI.XWPF.UserModel;\n",
    "using System.IO;\n",
    "\n",
    "var doc = new XWPFDocument();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "XWPFParagraph paragraph = doc.CreateParagraph();\n",
    "XWPFRun run = paragraph.CreateRun();\n",
    "//the text before the hyperlink\n",
    "run.SetText(\"This is a text paragraph having \");\n",
    "\n",
    "String rId = paragraph.Document.GetPackagePart().AddExternalRelationship(\n",
    "              \"https://www.bing.com\",\n",
    "              XWPFRelation.HYPERLINK.Relation).Id;\n",
    "var hyperlinkrun =paragraph.CreateHyperlinkRun(rId);\n",
    "hyperlinkrun.SetText(\"a link to Bing\");\n",
    "hyperlinkrun.SetColor(\"0000FF\");\n",
    "hyperlinkrun.Underline = UnderlinePatterns.Single;\n",
    "\n",
    "//the text after the hyperlink\n",
    "run = paragraph.CreateRun();\n",
    "run.SetText(\" in it.\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "//save the word document to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "using (var fs = new FileStream(\"sample9.docx\", FileMode.Create))\n",
    "{\n",
    "    doc.Write(fs);\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "python"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
